package com.ybkj.daijia.server.event.listener.errand;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.errand.OrderUp;
import com.ybkj.daijia.server.event.model.errand.ErrandOrderUpRunEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.service.AreaService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.ErrandService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsService;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ErrandOrderUpRunEventListener implements ApplicationListener<ErrandOrderUpRunEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ErrandOrderUpRunEventListener.class);


    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private ErrandService errandService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SmsService smsService;

    @Override
    @Async
    public void onApplicationEvent(ErrandOrderUpRunEvent event) {
        OrderUp order = (OrderUp) event.getSource();
        Passenger passenger = event.getPassenger();

        logger.debug("order errandRun event:{}", order.getOrderNo());

        //给客户推送
        passenger.setCid(memberService.findCid(passenger.getId()));
        if (StringUtils.isNotBlank(passenger.getCid())) {
            try {
                //发送设备推送
                PushData pushData = new PushData();
                pushData.setType("RUN_ORDER_ERRAND");
                pushData.setTitle("跑腿开始执行");
                pushData.setDescription("服务人员已经开始执行您的订单,单号:" + order.getOrderNo());
                pushData.setData(order.getId());

                GeTuiMember.push(passenger, pushData, settingUtils.get());
            } catch (IOException e) {
                logger.debug("push error : " + e.getMessage());
            }
        }
    }

}
